package com.android.lovely.ui.fragment.role

import android.view.ViewGroup.MarginLayoutParams
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import com.android.lovely.R
import com.android.lovely.databinding.FragmentCreateRoleThreeBinding
import com.android.lovely.room.role.CreateRole
import com.android.lovely.ui.viewmodel.CreateRoleViewModel
import com.android.lovely.ui.widget.CreateRoleBottomWidget.OnBottomClickListener
import com.android.lovely.ui.widget.RoleTipWidget
import com.kotlin.extensions.coroutine.CoroutineExt.launchSafety
import com.kotlin.extensions.dimen.Dimension.dp
import com.kotlin.extensions.view.ViewExt.clickable
import kotlinx.coroutines.flow.collectLatest

/**
 *
 * @author why
 * @date 2025/4/5 21:20
 */
class CreateRoleStep3Fragment: CreateRoleBaseFragment<FragmentCreateRoleThreeBinding>() {

    val adapter by lazy {
        KeyWordAdapter()
    }

    override fun initView() {
        mBinding.clBottom.update(viewModel.isEdit, object : OnBottomClickListener{
            override fun onPreview() {
                save()
                if (viewModel.isEdit) {
                    viewLifecycleOwner.lifecycleScope.launchSafety {
                        viewModel.save()
                        activity?.finish()
                    }
                } else {
                    viewModel.nextStep(2)
                }
            }

            override fun onNext() {
                save()
                if (viewModel.isEdit) {
                    viewLifecycleOwner.lifecycleScope.launchSafety {
                        viewModel.save()
                        activity?.finish()
                    }
                } else {
                    viewModel.nextStep(2)
                }
            }

            override fun onBack() {
                viewModel.preStep(2)
            }
        })

        getString(R.string._0_5000_min_200, "0").also { mBinding.characterContentNum.text = it }
        getString(R.string._0_1000_min_100, "0").also { mBinding.scenarioContentNum.text = it }

        mBinding.characterContent.addTextChangedListener {
            getString(R.string._0_5000_min_200, (it?.length.toString())).also { mBinding.characterContentNum.text = it }
            checkEnable()
        }
        mBinding.scenarioContent.addTextChangedListener {
            getString(R.string._0_1000_min_100, (it?.length.toString())).also { mBinding.scenarioContentNum.text = it }
            checkEnable()
        }
        mBinding.clTip.setListener(object : RoleTipWidget.OnClickListener {
            override fun onClick(text: String) {
                if( mBinding.characterContent.hasFocus()){
                    mBinding.characterContent.text?.insert(maxOf(mBinding.characterContent.selectionEnd, 0), text)
                }

                if( mBinding.scenarioContent.hasFocus()){
                    mBinding.scenarioContent.text?.insert(maxOf(mBinding.scenarioContent.selectionEnd, 0), text)
                }
            }
        })
    }

    override fun update(it: CreateRole) {
        mBinding.characterContent.setText(it.description)
        mBinding.scenarioContent.setText(it.scenario)
        mBinding.clBottom.update(viewModel.isEdit)
        checkEnable()
    }

    private fun checkEnable() {
        var isEnable = true

        if ((mBinding.characterContent.text?.trim()?.length ?: 0) < 200) {
            isEnable = false
        }
        val scenarioLength = (mBinding.scenarioContent.text?.trim()?.length ?: 0)
        if (scenarioLength != 0 && scenarioLength < 100) {
            isEnable = false
        }
        mBinding.clBottom.updateEnable(isEnable)
    }

    override fun save() {
        viewModel.updateRole {
            description = mBinding.characterContent.text.toString()
            scenario = mBinding.scenarioContent.text.toString()
        }
    }

    override fun hasEdit(): Boolean {
        return viewModel.createRole?.description != mBinding.characterContent.text.toString()
                || viewModel.createRole?.scenario != mBinding.scenarioContent.text.toString()
    }

    override fun onKeyBoardChange(progress: Int) {
        mBinding.clTip.updateLayoutParams<MarginLayoutParams> {
            bottomMargin = (progress - bottomHeight).coerceAtLeast(0)
        }
        mBinding.clTip.isVisible = progress > 0

    }


    override suspend fun initData() {

    }
}